//
//  XMSettingCell.m
//  XMWeibo
//
//  Created by chi on 14-9-1.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "XMSettingCell.h"

#import "XMBadgeView.h"

#import "XMArrowSettingItem.h"
#import "XMLabelSettingItem.h"
#import "XMSwitchSettingItem.h"
#import "XMCheckSettingItem.h"


@interface XMSettingCell ()

/** 开关 */
@property (nonatomic, strong) UISwitch *accessorySwitch;

/** Label */
@property (nonatomic, strong) UILabel *accessoryLabel;

/** ArrowView */
@property (nonatomic, strong) UIImageView *arrowImageView;

/** CheckView */
@property (nonatomic, strong) UIImageView *checkImageView;

/** 提示视图 */
@property (nonatomic, strong) XMBadgeView *badgeView;

/** 选中cell背景view */
@property (nonatomic, weak) UIImageView *selectedBgView;

/** cell背景view */
@property (nonatomic, weak) UIImageView *bgView;;

@end

@implementation XMSettingCell


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];

        self.detailTextLabel.font = [UIFont systemFontOfSize:13.0];
        self.detailTextLabel.textColor = [UIColor lightGrayColor];
        self.detailTextLabel.highlightedTextColor = [UIColor lightGrayColor];
        
        
        self.textLabel.highlightedTextColor = [UIColor blackColor];
        
        }
    return self;
}

#pragma mark - 懒加载控件

/**
 *  开关
 */
- (UISwitch *)accessorySwitch
{
    if (_accessorySwitch == nil) {
        
        _accessorySwitch = [[UISwitch alloc]init];
        [_accessorySwitch addTarget:self action:@selector(switchValueChange:) forControlEvents:UIControlEventValueChanged];
    }
    
    return _accessorySwitch;
}

- (void)switchValueChange:(UISwitch*)sender
{
    ((XMSwitchSettingItem*)_cellItem).isOn = sender.isOn;
}

/**
 *  Label
 */
- (UILabel *)accessoryLabel
{
    if (_accessoryLabel == nil) {
        UILabel *label = [[UILabel alloc]init];
        label.font = [UIFont systemFontOfSize:13.0];
        label.textColor = [UIColor grayColor];
        self.accessoryLabel = label;
    }
    
    return _accessoryLabel;
}

- (void)setRightLableText:(NSString*)text
{
    CGSize size = [text sizeWithFont:self.accessoryLabel.font];
    
    CGRect bounds = (CGRect){CGPointZero, size};
    
    self.accessoryLabel.bounds = bounds;
    self.accessoryLabel.text = text;
}


- (UIImageView *)checkImageView
{
    if (_checkImageView == nil) {
        _checkImageView = [[UIImageView alloc]init];
        _checkImageView.image = [UIImage adaptIOS7ImageWithName:@"common_icon_checkmark"];
        _checkImageView.bounds = (CGRect){CGPointZero, _checkImageView.image.size};
    }
    
    return _checkImageView;
}

/**
 *  箭头view
 */
- (UIImageView *)arrowImageView
{
    if (_arrowImageView == nil) {
        _arrowImageView = [[UIImageView alloc]init];
        _arrowImageView.image = [UIImage adaptIOS7ImageWithName:@"common_icon_arrow"];
        _arrowImageView.bounds = (CGRect){CGPointZero, _arrowImageView.image.size};
    }
    
    return _arrowImageView;
}


- (XMBadgeView *)badgeView
{
    if (_badgeView == nil) {
        _badgeView = [[XMBadgeView alloc]init];
    }
    
    return _badgeView;
}

- (UIImageView *)bgView
{
    if (_bgView == nil) {
        UIImageView *view = [[UIImageView alloc]init];
        _bgView = view;
        self.backgroundView = _bgView;
    }
    
    return _bgView;
}

- (UIImageView *)selectedBgView
{
    if (_selectedBgView == nil) {
        UIImageView *view = [[UIImageView alloc]init];
        _selectedBgView = view;
        self.selectedBackgroundView = view;
    }
    
    return _selectedBgView;
}

#pragma mark -



- (void)setCellItem:(XMBaseSettingItem *)cellItem
{
    _cellItem = cellItem;
    
    self.textLabel.text = cellItem.title;
    self.detailTextLabel.text = cellItem.subtitle;
    
    if (cellItem.icon) {
        self.imageView.image = [UIImage imageNamed:cellItem.icon];
    }
    
    if (cellItem.badgeValue) {
        [self.badgeView setBadgeValue:cellItem.badgeValue];
        self.accessoryView = self.badgeView;
    }
    else if ([cellItem isKindOfClass:[XMCheckSettingItem class]]) {
        XMCheckSettingItem *checkItem = (XMCheckSettingItem*)cellItem;
        if (checkItem.isChecked) {
            self.accessoryView = self.checkImageView;
        }
        else {
            self.accessoryView = nil;
        }
        
    }
    else if ([cellItem isKindOfClass:[XMLabelSettingItem class]]){
        XMLabelSettingItem *tItem = (XMLabelSettingItem*)cellItem;
        [self setRightLableText:tItem.labelText];
        self.accessoryView = self.accessoryLabel;
    }
    else if ([cellItem isKindOfClass:[XMArrowSettingItem class]]) {
        self.accessoryView = self.arrowImageView;
    }
    else if ([cellItem isKindOfClass:[XMSwitchSettingItem class]]) {
        XMSwitchSettingItem *switchItem = (XMSwitchSettingItem*)cellItem;
        self.accessorySwitch.on = switchItem.isOn;
        self.accessoryView = self.accessorySwitch;
        
    }
    
    else {
        self.accessoryView = nil;
    }

}


/**
 *  获得cell
 */
+ (instancetype)cellWithTableView:(UITableView*)tableView indexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseIdentifier = @"settingCell";
    
    XMSettingCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:reuseIdentifier];
    
    
    if (cell == nil) {
        cell = [[XMSettingCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
    }
    
    if (1 == [tableView numberOfRowsInSection:indexPath.section]) {
        cell.bgView.image = [UIImage stretchableImageWithName:@"common_card_background"];
        cell.selectedBgView.image =  [UIImage stretchableImageWithName:@"common_card_background_highlighted"];

    }
    else if (indexPath.row == 0) {
        cell.bgView.image = [UIImage stretchableImageWithName:@"common_card_top_background"];
        cell.selectedBgView.image = [UIImage stretchableImageWithName:@"common_card_top_background_highlighted"];
    }
    else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1) {
        cell.bgView.image = [UIImage stretchableImageWithName:@"common_card_bottom_background"];
        cell.selectedBgView.image = [UIImage stretchableImageWithName:@"common_card_bottom_background_highlighted"];
    }
    else {
        cell.bgView.image = [UIImage stretchableImageWithName:@"common_card_middle_background"];
        cell.selectedBgView.image = [UIImage stretchableImageWithName:@"common_card_middle_background_highlighted"];
    }
    
    
    return cell;
    
}

- (void)setFrame:(CGRect)frame
{
    if (isIOS7) {
        CGFloat edge = 5.0;
        frame.origin.x += edge;
        frame.size.width -= 2 * edge;
    }
    
    [super setFrame:frame];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    
    if (self.detailTextLabel.text.length > 0) {
        CGRect detailFrame = self.detailTextLabel.frame;
        
        CGRect titleFrame = self.textLabel.frame;
        
        detailFrame.origin.x = CGRectGetMaxX(titleFrame) + 3.0;
        detailFrame.origin.y -= 2.0;
        
        self.detailTextLabel.frame = detailFrame;
        
    }
    
    
}

@end
